package com.orangelife.arenasdk.main;

import android.content.Context;
import android.util.Log;

import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.JSONHelper;
import com.curry.android.util.ToastHelper;
import com.orangelife.arenasdk.biz.ArenaBiz;
import com.orangelife.arenasdk.util.EncryptionUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * Created by 吴格非 on 2016-10-9.
 * <p>
 *
 * @author 吴格非
 * @since v1.0.0
 */

public class Arena {

    private static Arena arena;

    public static Arena getInstance() {
        if (arena == null) {
            arena = new Arena();
        }
        return arena;
    }

    private Arena() {

    }

    private String crypto;
    private String roomID;

    public void initialize(Context context, String host) {
        ArenaBiz.HOST = host;
        VolleyHttpUtil.getInstance().initVolleyHttpUtil(context);
        OrangeErrorHandler.getInstance().initOrangeHttpBiz(context);
        ToastHelper.getInstance().init(context);
    }

    public void init(final String functionName, final String seqID, String gameID, String token, String commID, String version, final EventCallback callback) {
//        token = "83658b95-e8ad-4aba-a56f-01f64a1a2ddb";
        VolleyHttpUtil.getInstance().setHeader("accessToken", token);
        VolleyHttpUtil.getInstance().setHeader("commID", commID);
        VolleyHttpUtil.getInstance().setHeader("app", "android");
        VolleyHttpUtil.getInstance().setHeader("ver", version);
        ArenaBiz.httpInit(gameID, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject jsonObject, int i) {
                if (OrangeErrorHandler.getInstance().isSuccess(jsonObject)) {
                    Map<String, Object> map = OrangeDataHandler.getMapWithEntity(jsonObject);
                    crypto = String.valueOf(map.get("crypto"));
                    Log.e("Arena", "crypto=" + crypto);
                    roomID = String.valueOf(map.get("roomID"));
                    try {
                        JSONObject jo1 = jsonObject.getJSONObject("entity");
                        jo1.remove("crypto");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
//                    String roomParams = String.valueOf(map.get("roomParams"));
//                    Map<String, Object> jsonMap = new HashMap<>();
//                    jsonMap.put("roomID", roomID);
//                    jsonMap.put("params", roomParams);
//                    String jsonParam = String.valueOf(JSONHelper.Map2Json(map));
                }
                handleResult(jsonObject, callback, functionName, seqID);
            }
        });
    }

    public void event(final String functionName, final String seqID, String json, final EventCallback callback) {//data callback
        Map<String, Object> map1 = JSONHelper.jsonToMap(json);
        if (map1 == null) {
            handleResult(null, callback, functionName, seqID);
            return;
        }
        String eventName = String.valueOf(map1.get("event"));
        Map<String, Object> paramsMap = (Map<String, Object>) map1.get("params");
        String paramsJson = JSONHelper.Map2Json(paramsMap);

        Map<String, Object> encryptMap = new HashMap<>();
        encryptMap.put("event", eventName);
        encryptMap.put("params", paramsJson);
        String data = null;
        try {
            data = EncryptionUtil.encrypt(crypto, encryptMap);
            Log.e(Arena.class.getSimpleName(), "data=" + data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (data != null) {
            ArenaBiz.httpEvent(data, roomID, new VolleyHttpUtil.ResponseCallBack() {
                @Override
                public void handleResponse(JSONObject jsonObject, int i) {
                    handleResult(jsonObject, callback, functionName, seqID);
                }
            });
        } else {
            handleResult(null, callback, functionName, seqID);
        }
    }


    public void rank(final String functionName, final String seqID, String params, final EventCallback callback) {//data callback
        String gameID = "";
        String pageNum = "";
        String pageSize = "";
        if (params != null) {
            Map<String, Object> map = JSONHelper.jsonToMap(params);
            gameID = String.valueOf(map.get("gameID"));
            pageNum = String.valueOf(map.get("pageIdex"));
            pageSize = String.valueOf(map.get("pageSize"));
        }
        ArenaBiz.httpRank(gameID, pageNum, pageSize, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject jsonObject, int i) {
                handleResult(jsonObject, callback, functionName, seqID);
            }
        });
    }

    private void handleResult(JSONObject jsonObject, EventCallback callback, String functionName, String seqID) {
        if (jsonObject != null) {
            callback.onFinish(functionName, seqID, "{\"StatusCode\":\"200\",\"info\":\"ok\",\"body\":" + jsonObject.toString() + "}");
        } else {
            callback.onFinish(functionName, seqID, "{\"StatusCode\":\"500\",\"info\":\" 服务器内部错误 !\"}");
        }
    }
}
